package com.mrx.pvzcommunity.service;

import com.mrx.pvzcommunity.dao.UserDao;
import com.mrx.pvzcommunity.model.Response;
import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.user.AvatarUser;
import com.mrx.pvzcommunity.model.user.NormalUser;
import com.mrx.pvzcommunity.model.user.User;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserService {

    private final UserDao userDao;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public Result<String> isUserValid(HttpSession session, User user) {
        if (isUserNotExists(user)) {
            return Result.createResult(Response.Code.LOG_FAILURE_ACCOUNT_NOT_EXISTS);
        }
        User me = userDao.findUserByUser(user);
        if (me == null) {
            return Result.createResult(Response.Code.LOG_FAILURE_ACCOUNT_OR_PASS_ERR);
        }
        logger.info(me.toString());
        session.setAttribute("user", me);
        return Result.createResult(Response.Code.SUCCESS);
    }

    public boolean isUserNotExists(User user) {
        if (user == null) {
            return true;
        }
        User me = userDao.isAccountExists(String.valueOf(user.getAccount()));
        return me == null;
    }

    public Result<String> register(HttpSession session, User user) {
        if (!user.canRegister()) {
            return Result.createResult(Response.Code.NEED_ARG);
        }
        try {
            boolean res = userDao.addNewUser(user);
            if (res) {
                session.setAttribute("user", user);
                return Result.createResult(Response.Code.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createResult(Response.Code.REG_FAILURE_INTERNAL_ERROR, e);
        }
        return Result.createResult(Response.Code.REG_FAILURE_UNKNOWN_ERROR);
    }

    public Result<List<NormalUser>> getFriendsByUid(String uid) {
        List<NormalUser> res = userDao.getFriendsById(uid);
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.NOT_FOUND, res);
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    public Result<List<NormalUser>> getFriendsByUser(User user) {
        if (user != null) {
            return getFriendsByUid(String.valueOf(user.getUid()));
        }
        return Result.createResult(Response.Code.USER_NOT_LOGGED, Collections.emptyList());
    }

    public Result<String> followUserByUid(User user, String fid) {
        if (user != null) {
            try {
                if (userDao.followUserByUid(String.valueOf(user.getUid()), fid)) {
                    return Result.createResult(Response.Code.SUCCESS);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.createResult(Response.Code.REG_FAILURE_INTERNAL_ERROR, e);
            }
        }
        return Result.createResult(Response.Code.USER_NOT_LOGGED);
    }

    public Result<String> getProblemByUId(String uid) {
        String res = userDao.getProblemByUid(uid);
        if (res == null) {
            return Result.createResult(Response.Code.NOT_FOUND);
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    public Result<User> checkUidAndAnswer(String uid, String answer) {
        User user = userDao.checkUidAndAnswer(uid, answer);
        if (user == null) {
            return Result.createResult(Response.Code.NOT_FOUND, (User) null);
        }
        return Result.createResult(Response.Code.SUCCESS, user);
    }

    public Result<AvatarUser> findUserByUid(String uid) {
        User user = userDao.findUserByUid(uid);
        if (user == null) {
            return Result.createResult(Response.Code.NOT_FOUND, (AvatarUser) null);
        }
        return Result.createResult(Response.Code.SUCCESS, user.toAvatarUser());
    }
}
